package cn.com.self.apis.portal.service.impl;

import cn.com.self.apis.portal.entity.SysMenuEntity;
import cn.com.self.apis.portal.entity.SysRoleMenuEntity;
import cn.com.self.apis.portal.entity.SysUserEntity;
import cn.com.self.apis.portal.entity.SysUserRoleEntity;

import cn.com.self.apis.portal.mapper.SysMenuMapper;
import cn.com.self.apis.portal.mapper.SysRoleMenuMapper;
import cn.com.self.apis.portal.mapper.SysUserMapper;
import cn.com.self.apis.portal.mapper.SysUserRoleMapper;
import cn.com.self.apis.portal.service.intf.ILoginService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 */
@Service
public class LoginService implements ILoginService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;


    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public  Map<String,Object>  login(String userName, String password) {
        Map<String, Object> rstMap = new HashMap<>();
        Map<String, String> lgMap = new HashMap<>();
        SysUserRoleEntity userRole = new SysUserRoleEntity();
        List<SysMenuEntity> list = new ArrayList<>();
        SysUserEntity info = new SysUserEntity();
        QueryWrapper<SysUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userName);
        queryWrapper.eq("password", password);
        info = sysUserMapper.selectOne(queryWrapper);
        //存在相同用户则登录成功
        if (info != null) {
            rstMap.put("code", "200");
            rstMap.put("text", "登录成功!");
            list = getUserPrivile(String.valueOf(info.getId()));

        } else {
            rstMap.put("code", "500");
            rstMap.put("text", "登录失败，用户名或密码错误!");

        }
        rstMap.put("userInfo", info);
        rstMap.put("userPrivile", list);

        return rstMap;
    }


    private List<SysMenuEntity> getUserPrivile(String userId){
        List<SysMenuEntity> MenuEntityList = new ArrayList<>();
        List<String> roleIds = new ArrayList<>();
        QueryWrapper<SysUserRoleEntity> queryWrapper = new QueryWrapper<>();
        QueryWrapper<SysRoleMenuEntity> rolePrivileWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        //获取用户拥有的角色
        List<SysUserRoleEntity> list = sysUserRoleMapper.selectList(queryWrapper);
        for (SysUserRoleEntity info:list){
            roleIds.add(info.getRoleId());
        }
        if (roleIds!=null&&roleIds.size()>0){
            rolePrivileWrapper.in("role_id",roleIds);
        }else{
            rolePrivileWrapper.in("role_id","");

        }
        //获取出角色与权限关联数据
        List<SysRoleMenuEntity> roleMenuEntityList = sysRoleMenuMapper.selectList(rolePrivileWrapper);

        for (SysRoleMenuEntity entity:roleMenuEntityList){
            QueryWrapper<SysMenuEntity>  privileWrapper = new QueryWrapper<>();
            privileWrapper.eq("id",entity.getMenuId());
            //获取角色权限信息
            SysMenuEntity info = sysMenuMapper.selectOne(privileWrapper);
            MenuEntityList.add(info);
        }

        return  MenuEntityList;
    }


}
